//
//  YXDownloadTableViewCell.m
//  MockProject
//
//  Created by 周丽丽 on 16/8/24.
//  Copyright © 2016年 niuzhaowang. All rights reserved.
//

#import "YXDownloadTableViewCell.h"
#import "YXDownloader.h"
@interface YXDownloadTableViewCell ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *downloadProgressLbl;
@property (nonatomic, strong) UIButton *downloadButton;
@property (nonatomic, strong) UILabel *downloadSpeedLabel;
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic, strong) UIProgressView *progressView;
@end
@implementation YXDownloadTableViewCell
- (void)dealloc{
    [self.displayLink invalidate];
    self.displayLink = nil;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupSubviews];
    }
    return  self;
}
- (void)setupSubviews{
    UILabel *titleLbl = [[UILabel alloc]init];
    titleLbl.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview: titleLbl];
    self.titleLabel = titleLbl;
    
    UILabel *downloadProgressLbl = [[UILabel alloc]init];
    downloadProgressLbl.text = @"102M";
    downloadProgressLbl.font = [UIFont systemFontOfSize:12];
    downloadProgressLbl.textColor = [UIColor colorWithHexString:@"a3a1a0"];
    [self.contentView addSubview:downloadProgressLbl];
    self.downloadProgressLbl = downloadProgressLbl;
    
    UILabel *downloadSpeedLbl = [[UILabel alloc]init];
    downloadSpeedLbl.font = [UIFont systemFontOfSize:12];
    downloadSpeedLbl.textAlignment = NSTextAlignmentRight;
    downloadSpeedLbl.textColor = [UIColor colorWithHexString:@"e59252"];
    [self.contentView addSubview:downloadSpeedLbl];
    self.downloadSpeedLabel = downloadSpeedLbl;
    
    UIButton *downloadBtn = [[UIButton alloc]init];
    [downloadBtn setImage:[UIImage imageNamed:@"圆角矩形-43"] forState:UIControlStateNormal];
    [downloadBtn setImage:[UIImage imageNamed:@"下载中icon点击态"] forState:UIControlStateHighlighted];
    [downloadBtn setTitleColor:[UIColor colorWithHexString:@"797675"] forState:UIControlStateNormal];
    downloadBtn.titleLabel.font = [UIFont systemFontOfSize:11];
    [self.contentView addSubview:downloadBtn];
    [downloadBtn addTarget:self action:@selector(downloadingButtonCLick:) forControlEvents:UIControlEventTouchUpInside];
    self.downloadButton = downloadBtn;
    
    UIProgressView *progressV = [[UIProgressView alloc]init];
    progressV.trackTintColor = [UIColor colorWithHexString:@"f5f5f5"];
    progressV.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:progressV];
    self.progressView = progressV;
    if (self.progressView.progress < 0.5) {
        self.progressView.progressTintColor = [UIColor colorWithHexString:@"ff8400"];
    }else if (self.progressView.progress >= 0.5){
        self.progressView.progressTintColor = [UIColor colorWithHexString:@"ffc01c"];
    }
}
- (void)downloadingButtonCLick:(UIButton *)sender{
    if (self.downloader.state == TaskStateDownloading) {
        [self.downloader pauseDownload];
    }else if (self.downloader.state == TaskStatePause){
        if (self.downloadButtonBlock) {
            self.downloadButtonBlock(self);
        }
    }else if (self.downloader.state == TaskStateWaiting){
        [self.downloader pauseDownload];
    }else if (self.downloader.state == TaskStateFialed){
                [self.downloader resumeDownload];
    }
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [self layoutFrame];
}
- (void)layoutFrame{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-90);
        make.bottom.equalTo(self.contentView.mas_centerY);
    }];
    [self.downloadProgressLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(13);
        make.left.equalTo(self.contentView).offset(15);
    }];
    [self.downloadSpeedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(13);
        make.right.equalTo(self.downloadButton.mas_left).offset(-6);
    }];
    [self.downloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).offset(-36);
    }];
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.bottom.equalTo(self.contentView).offset(-5);
        make.right.equalTo(self.contentView).offset(-15);
        make.height.mas_equalTo(2);
    }];
}
- (void)setDownloader:(YXDownloader *)downloader{
    _downloader = downloader;
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)handleDisplayLink:(CADisplayLink *)diaplayLink{
    [self settingData];
}
- (void)settingData{
    self.titleLabel.text = self.downloader.title;
    self.downloadProgressLbl.text = [NSString stringWithFormat:@"%.2fMB/%.2fMB",self.downloader.downloadSize,self.downloader.totalSize];
    self.downloadSpeedLabel.text = self.downloader.speed;
    self.progressView.progress = self.downloader.downloadProgress;
    NSString *state = [NSString string];
    switch (self.downloader.state) {
        case TaskStateWaiting:
            state = @"等待中";
            break;
        case TaskStateDownloading:
            state = @"下载中";
            break;
        case TaskStatePause:
            state = @"已暂停";
            break;
        case TaskStateFialed:
            state = @"下载失败";
            break;
        case TaskStateComplete:
            state = @"下载完成";
            break;
        default:
            break;
    }
    [self.downloadButton setTitle:state forState:UIControlStateNormal];
}
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillRect(context, rect);
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:@"e0dcd9"].CGColor);
    CGContextStrokeRect(context, CGRectMake(15, rect.size.height, rect.size.width - 10, 1));
}
@end
